package ebj.common.document_create.helper;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;

public class MyHeaderFooter extends PdfPageEventHelper{
    Font header_font = new XMLWorkerFontProvider().getFont("宋体", 10, Font.BOLD, BaseColor.BLACK);
    Font footer_font = new XMLWorkerFontProvider().getFont("宋体", 12, BaseColor.RED);
    PdfTemplate tmpl_totalPageNmbDesc;
    String courseName;
    String title;
    
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        // TODO Auto-generated method stub
        super.onOpenDocument(writer, document);
        tmpl_totalPageNmbDesc = writer.getDirectContent().createTemplate(30, 16);
    }

    @Override
    public void onEndPage (PdfWriter writer, Document document) {
        /*
        Rectangle rect = writer.getBoxSize("art");
        rect.setBorder(Rectangle.BOTTOM);
        Font headerFooterFont = ITextHelper.getChineseFont(10, Font.BOLD);
        switch(writer.getPageNumber() % 2) {
        case 0:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_RIGHT, new Phrase("event-header   偶数页-页眉-内容", headerFooterFont ),
                    rect.getRight(), rect.getTop(), 0);
            break;
        case 1:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_LEFT, new Phrase("odd-header  奇数页-页眉-内容", headerFooterFont),
                    rect.getLeft(), rect.getTop(), 0);
            break;
        }
        */
        float header_footer_total_width = PageSize.A4.getWidth() - 100;
        int[] tableCellWidths = new int[] { 24, 24, 3};
        float header_footer_xPos = 50;
        float header_footer_yPos = 39;
        PdfPTable tbl_header = new PdfPTable(3);
        
        try {  
            tbl_header.setTotalWidth(header_footer_total_width);  
            tbl_header.setWidths(tableCellWidths);  
            tbl_header.setLockedWidth(true);  
            tbl_header.getDefaultCell().setFixedHeight(20);  
            tbl_header.getDefaultCell().setBorder(Rectangle.BOTTOM);  
            
            tbl_header.addCell(new Paragraph(this.title, header_font));// 可以直接使用addCell(str)，不过不能指定字体，中文无法显示  
            
//            tbl_header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);  
//            tbl_header.addCell(new Paragraph("第" + writer.getPageNumber() + "页 |", font));  
            tbl_header.addCell(new Phrase(""));  
            
            // Logo
            Image image = Image
                    .getInstance("D:\\workspace\\jee_luna\\jse-common\\"
                            + "FinanceAssociation2.jpg");

            image.scaleAbsolute(19, 19);
//            image.scalePercent(10); // 显示原图片大小的20%
            image.setAlignment(Image.MIDDLE); 
            PdfPCell cell = new PdfPCell(image);  
            cell.setBorder(Rectangle.BOTTOM);  
            tbl_header.addCell(cell);  
   
            // 将页眉写到document中，位置可以指定，指定到下面就是页脚  
            tbl_header.writeSelectedRows(0, -1, header_footer_xPos,PageSize.A4.getHeight() - 20, writer.getDirectContent());  
        } catch (Exception de) {  
            throw new ExceptionConverter(de);  
        }
        
        
        
        PdfPTable tbl_footer = new PdfPTable(3);
        try {  
            tbl_footer.setTotalWidth(header_footer_total_width);  
            tbl_footer.setWidths(tableCellWidths);  
            tbl_footer.setLockedWidth(true);  
            tbl_footer.getDefaultCell().setFixedHeight(-12);  
            tbl_footer.getDefaultCell().setBorder(Rectangle.TOP);  
   
            tbl_footer.addCell(new Paragraph(this.courseName, footer_font));// 可以直接使用addCell(str)，不过不能指定字体，中文无法显示  
            
            tbl_footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);  
            tbl_footer.addCell(new Paragraph("第" + writer.getPageNumber() + "页 |", footer_font));  
   
            // 总页数  
            PdfPCell cell = new PdfPCell(Image.getInstance(tmpl_totalPageNmbDesc));  
            cell.setBorder(Rectangle.TOP);  
            tbl_footer.addCell(cell);  
   
            // 将页眉写到document中，位置可以指定，指定到下面就是页脚  
//            table.writeSelectedRows(0, -1, 50,PageSize.A4.getHeight() - 20, writer.getDirectContent());  
            tbl_footer.writeSelectedRows(0, -1, header_footer_xPos, header_footer_yPos, writer.getDirectContent());  
        } catch (Exception de) {  
            throw new ExceptionConverter(de);  
        }
    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        // TODO Auto-generated method stub
        super.onCloseDocument(writer, document);
        String text = "共" + String.valueOf(writer.getPageNumber()) + "页";  
        ColumnText.showTextAligned(tmpl_totalPageNmbDesc, Element.ALIGN_LEFT, new Paragraph(text,footer_font), 2, 2, 0);
    }
}